<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <span>总记录数: {{ total }} 条</span>
        </template>

        <template #right>
          <el-button type="warning" size="small" @click="$router.push('/import?type=user')">excel导入</el-button>
          <el-button type="danger" size="small">excel导出</el-button>
          <el-button type="primary" size="small" @click="showDialog=true">新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 10px;">
        <el-table v-loading="loading" border :data="list">
          <el-table-column label="序号" type="index" sortable="" width="80" />
          <el-table-column label="姓名" prop="username" sortable="" />
          <el-table-column label="手机号" prop="mobile" sortable="" />
          <el-table-column label="工号" prop="workNumber" sortable="" />
          <el-table-column label="聘用形式" prop="formOfEmployment" :formatter="formatterFn" sortable="" />
          <el-table-column label="部门" prop="departmentName" sortable="" />
          <el-table-column label="入职时间" prop="timeOfEntry" sortable="">
            <template #default="{row}">
              {{ formatDate(row.timeOfEntry,'/') }}
            </template>
          </el-table-column>
          <el-table-column label="操作" sortable="" fixed="right" width="280">
            <template #default="{row}">
              <el-button type="text" size="small" @click="$router.push(`/employees/detail/${row.id}`)">查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button type="text" size="small">角色</el-button>
              <el-button type="text" size="small" @click="del(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <div style="height: 60px; margin-top: 10px">
          <el-pagination
            background
            :current-page="page"
            :page-size="pageSize"
            layout="prev, pager, next"
            :total="total"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-card>
    </div>
    <add-employee :show-dialog.sync="showDialog" @add-employee="getEmployeeList" />
  </div>
</template>

<script>
import { reqGetEmployeeList, reqDelEmployee } from '@/api/employees'
import EmployeeEnum from '@/api/constant/employees'
import { formatDate } from '@/filters'
import AddEmployee from './components/add-employee.vue'
export default {
  components: { AddEmployee },
  data() {
    return {
      showRoleDialog: false,
      showDialog: false,
      loading: false,
      list: [],
      page: 1,
      pageSize: 10,
      total: 0
    }
  },
  computed: {
    type() {
      return this.$route.query.type
    }
  },
  created() {
    this.getEmployeeList()
  },
  methods: {
    del(id) {
      this.$confirm('确定要删除该员工?', '温馨提示').then(async() => {
        if (this.list.length === 1 && this.page > 1) {
          this.page--
        }
        await reqDelEmployee(id)
        this.$message.success('删除成功')
        this.getEmployeeList()
      }).catch(() => {
        console.log('取消')
      })
    },
    formatDate,
    async getEmployeeList() {
      this.loading = true
      const { data: { rows, total }} = await reqGetEmployeeList(this.page, this.pageSize)
      // console.log(rows)
      this.list = rows
      this.total = total
      this.loading = false
    },
    formatterFn(row, column, cellValue, index) {
      // console.log({ row, column, cellValue, index })
      const obj = EmployeeEnum.hireType.find(item => item.id === cellValue)?.value || '未知'
      // console.log(obj)
      return obj
    },
    handleCurrentChange(newPage) {
      // console.log(newPage)
      this.page = newPage
      this.getEmployeeList()
    }
  }
}
</script>

<style>

</style>
